package application;

import java.io.IOException;
import java.util.List;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import jeopardy.common.protocol.useraccount.UserAccountConsts;
import jeopardy.common.protocol.useraccount.UserAccountRequest;
import jeopardy.common.protocol.useraccount.UserAccountUtils;

public class PreGameScreenLogic extends Thread {

	private static int authenticated;
	private static int connected;
	private static String passWord;
	private static char[] userName;
	String SERVER_IP; 
	static int SERVER_PORT;
	private static SSLSocket statSocket;
	public static byte[] returnCode;
	static UserSession sessionU;
	static PreGameScreenSession sessionG;

	/* 
	 * This is the default constructor that will
	 * handle setting variables and instantiating objects
	 */
	public PreGameScreenLogic(){
		authenticated = 0;
		connected = 0;
		passWord = null;
		userName = null;
		statSocket = null;
		String SERVER_IP = null;
		SERVER_PORT = -1;
		returnCode = new byte[UserAccountConsts.USERACCOUNT_MAX_RES_LEN];
		sessionU = UserSession.getInstance();
	}
	
	public static void main() throws Exception

	{
		System.out.println("PreGameScreenLogic::main - executing fetchtPreGameStatsAndLists");
		PreGameScreenFetchData myFetcher = new PreGameScreenFetchData();
		// FetchGameListAndStats.copyLists(availableGames);
		Thread fetchThread = new Thread(myFetcher);
		fetchThread.start();
		sessionG = PreGameScreenSession.getInstance();
		sessionG.printList();
		
		// Close socket for stats
		closeSocket();

	}
	
	/*
	 * This method is used to create socket to communicate with the server
	 */
	public static int createSocket(int type)
	{
		// byte[] returnCode = new byte[UserAccountConsts.USERACCOUNT_MAX_RES_LEN];
		switch(type) {
			
			case 1: SERVER_PORT = ClientConsts.SERVER_PORT_USER_ACCOUNT;
					break;
			case 2: SERVER_PORT = ClientConsts.SERVER_PORT_STATS;
					break;
			case 3: SERVER_PORT = ClientConsts.SERVER_PORT_GAME;
					break;
			default: SERVER_PORT = -9000;
					break;
		}
		try {
			SSLSocketFactory sslsocketfactory = ClientConsts.createSSLFactory();
			statSocket = (SSLSocket) sslsocketfactory.createSocket(
						ClientConsts.SERVER_IP, SERVER_PORT);
			connected = 1;
			return 0;
			
		} catch (Exception exception) {
			exception.printStackTrace();
			System.out.println("Failed to create socket");
			connected = 0;
	        return 1;
		}	
	}

	/*
	 * This method is used to read information from the socket
	 */
	public static int readCommSocket( )
	{
		return 0;
		
	}
	/*
	 * This method is used to send information to the server
	 */
	public static int writeCommSocket(UserAccountRequest uar)
	{
		// byte[] returnCode = new byte[UserAccountConsts.USERACCOUNT_MAX_RES_LEN];
		try {
			statSocket.getOutputStream().write(
					UserAccountUtils.reqToNetworkMsg(uar));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			statSocket.getOutputStream().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}	

	public static int closeSocket()
	{
		int ret = 0;
		try {
			statSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(statSocket.isClosed()) {
			ret = 1;
		}
		
		return ret;
	}
}
